'use strict'

define(['jquery', 'bootstrap', 'select2', 'datetimepicker', 'datetimepickerCN',
        'shiftform', 'validate', 'validate.en'], function($) {
  
  return {
        
    init: function() {
        this.initCreate();
        this.initDelete();
        this.initDietEdit();
    },
    
    initCreate: function(){
        var that = this;
        
        $(document).on("shown.bs.modal","#create, #edit", function(e) {
            var $form = $(e.currentTarget).find("form");
            
              $form.bt_validate();
              
              $form.shiftform({
                beforeSubmit : function(el) {
                	var $groupAmount = $('input[name="groupAmount"]');
                	var $movementNumber = $('input[name="movementNumber"]');
                	if(!$groupAmount.val()){
                		$groupAmount.val(0);
                    }
                    if(!$movementNumber.val()){
                    	$movementNumber.val(0);
                    }
                    if(isNaN($groupAmount.val()) || isNaN($movementNumber.val())) {
                    	return false;
                    }
                  $('form').trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              });
        })
             
    },
    
    initDelete: function(){
        $(document).on("shown.bs.modal","#rest, #delete, #snack-delete", function(e) {
        	var $form = $(e.currentTarget).find("form");
            
            $form.shiftform();
        })
             
    },
    
    initDietEdit: function(){
        var that = this;
        
        $(document).on("shown.bs.modal","#diet-edit, #snack-create, #snack-edit", function(e) {
            var $form = $(e.currentTarget).find("form");
            
              $form.bt_validate();
              
              $form.shiftform({
                beforeSubmit : function(el) {
                  $('form').trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              });
        })
             
    },
    
   }
})
